package com.celesea.workflow.demo.vo;

import com.celesea.framework.util.CollectionKit;
import com.celesea.framework.vo.DictionaryVo;
import com.celesea.workflow.demo.entity.Demo;
import com.celesea.workflow.demo.entity.DemoData;
import com.celesea.workflow.demo.entity.DemoPerm;
import com.celesea.workflow.demo.v1.controller.request.PostRequest;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * @author JackYuan
 * @create 2017-11-28 16:59
 */
public class DemoVo extends Demo {
    private String id;
    private String title;
    private String content;
    private List<DictionaryVo> users;
    private List<DictionaryVo> groups;

    public DemoVo() {
    }

    public DemoVo(Demo demo) {
        BeanUtils.copyProperties(demo, this);
    }

    public DemoVo(PostRequest request) {
        BeanUtils.copyProperties(request, this);
    }

    public Demo toEntity() {
        Demo entity = new Demo();
        BeanUtils.copyProperties(this, entity);
        return entity;
    }

    public DemoData toEntityData() {
        DemoData entity = new DemoData();
        BeanUtils.copyProperties(this, entity);
        return entity;
    }

    public void setDemoData(DemoData demoData) {
        if (demoData != null) {
            this.title = demoData.getTitle();
            this.content = demoData.getContent();
        }
    }

    public void setDemoPerms(List<DemoPerm> demoPerms) {
        if (CollectionKit.isNotEmpty(demoPerms)) {
            this.users = Lists.newArrayList();
            this.groups = Lists.newArrayList();
            demoPerms.forEach(item -> {
                DictionaryVo dic = new DictionaryVo(item.getPermId(), item.getPermName());
                if (DemoPerm.PERM_TYPE_USER == item.getPermType()) {
                    this.users.add(dic);
                } else {
                    this.groups.add(dic);
                }
            });
        }
    }

    public List<DemoPerm> getDemoPerms() {
        List<DemoPerm> demoPerms = Lists.newArrayList();
        if (CollectionKit.isNotEmpty(users)) {
            users.forEach(item -> {
                demoPerms.add(new DemoPerm(this.id, DemoPerm.PERM_TYPE_USER, item));
            });
        }
        if (CollectionKit.isNotEmpty(groups)) {
            groups.forEach(item -> {
                demoPerms.add(new DemoPerm(this.id, DemoPerm.PERM_TYPE_GROUP, item));
            });
        }
        return demoPerms;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<DictionaryVo> getGroups() {
        return groups;
    }

    public void setGroups(List<DictionaryVo> groups) {
        this.groups = groups;
    }

    public List<DictionaryVo> getUsers() {
        return users;
    }

    public void setUsers(List<DictionaryVo> users) {
        this.users = users;
    }

}
